package com.example.utils;

import com.example.entity.*;

import java.util.List;
import java.util.Set;

/**
 *
 */
public class AddRelation {

    public static void addYM(Year yearNode, Month monthNode) {
        if (monthNode.getId() == null) {
            GuanXi_YM guanXi_ym = new GuanXi_YM("INCLUDE", monthNode);
            yearNode.addGuanXi_ym(guanXi_ym);
            return;
        }
        Set<GuanXi_YM> guanXi_ymSet = yearNode.getGuanXi_ymSet();
        boolean isExistGuanXi = false;
        for (GuanXi_YM ym:guanXi_ymSet) {
            Long exitId = ym.getMonth().getId();
            if (exitId.longValue() == monthNode.getId().longValue()) {
                isExistGuanXi = true;
                break;
            }
        }
        if (isExistGuanXi == false) {
            GuanXi_YM guanXi_ym = new GuanXi_YM("INCLUDE", monthNode);
            yearNode.addGuanXi_ym(guanXi_ym);
        }
    }

    public static void addEA(Event eventNode, Address addressNode) {
        if (addressNode.getId() == null) {
            GuanXi_EA guanXi_ea = new GuanXi_EA("LOCATION_IN", addressNode);
            eventNode.addGuanXi_ea(guanXi_ea);
            return;
        }
        // 遍历查找
        List<GuanXi_EA> guanXi_eaList = eventNode.getGuanXi_eaList();
        boolean isExistGuanXi = false;
        for (GuanXi_EA ea:guanXi_eaList) {
            Long exist_id = ea.getAddress().getId();
            if (exist_id.longValue() == addressNode.getId().longValue()) {
                isExistGuanXi = true;
                break;
            }
        }
        if (isExistGuanXi == false) {
            GuanXi_EA guanXi_ea = new GuanXi_EA("LOCATION_IN", addressNode);
            eventNode.addGuanXi_ea(guanXi_ea);
        }
    }

    public static void addEP(Event eventNode, People peopleNode) {
        if (peopleNode.getId() == null) {
            GuanXi_EP guanXi_ep = new GuanXi_EP("WHO", peopleNode);
            eventNode.addGuanXi_ep(guanXi_ep);
            return;
        }
        // 遍历查找
        List<GuanXi_EP> guanXi_epList = eventNode.getGuanXi_epList();
        boolean isExistGuanXi = false;
        for (GuanXi_EP ep:guanXi_epList) {
            Long exist_id = ep.getPeople().getId();
            if (exist_id.longValue() == peopleNode.getId().longValue()) {
                isExistGuanXi = true;
                break;
            }
        }
        if (isExistGuanXi == false) {
            GuanXi_EP guanXi_ep = new GuanXi_EP("WHO", peopleNode);
            eventNode.addGuanXi_ep(guanXi_ep);
        }
    }


    public static void addEK(Event eventNode, KnowledgeElement knowledgeElementNode) {
        if (knowledgeElementNode.getId() == null) {
            GuanXi_EK guanXi_ek = new GuanXi_EK("KE", knowledgeElementNode);
            eventNode.addGuanXi_ek(guanXi_ek);
            return;
        }

        List<GuanXi_EK> guanXi_ekList = eventNode.getGuanXi_ekList();
        boolean isExistGuanXi = false;
        for (GuanXi_EK ek:guanXi_ekList) {
            Long exist_id = ek.getKnowledgeElement().getId();
            if (exist_id.longValue() == knowledgeElementNode.getId().longValue()) {
                isExistGuanXi = true;
                break;
            }
        }
        if (isExistGuanXi == false) {
            GuanXi_EK guanXi_ek = new GuanXi_EK("KE", knowledgeElementNode);
            eventNode.addGuanXi_ek(guanXi_ek);
        }
    }

    public static void addEM(Event eventNode, Month month) {
        if (month.getId() == null) {
            GuanXi_EM guanXi_em = new GuanXi_EM("WHEN_IN", month);
            eventNode.addGuanXi_em(guanXi_em);
            return;
        }
        List<GuanXi_EM> guanXi_emList = eventNode.getGuanXi_emList();
        boolean isExistGuanXi = false;
        for (GuanXi_EM em:guanXi_emList) {
            Long exist_id = em.getMonth().getId();
            if (exist_id.longValue() == month.getId().longValue()) {
                isExistGuanXi = true;
                break;
            }
        }
        if (isExistGuanXi == false) {
            GuanXi_EM guanXi_em = new GuanXi_EM("WHEN_IN", month);
            eventNode.addGuanXi_em(guanXi_em);
        }
    }
}
